﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SinaWeiboApp_sl.Command
{
    public static class Click
    {
        private static readonly DependencyProperty ButtonClickCommandBehaviorProperty = DependencyProperty.RegisterAttached(
            "ButtonClickBehavior",
            typeof(ButtonClickBehavior),
            typeof(Click),
            null);

        public static readonly DependencyProperty ButtonClickCommandProperty = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(Click),
            new PropertyMetadata(OnSetCommandCallback));

        public static ICommand GetCommand(Button button)
        {
            return button.GetValue(ButtonClickCommandProperty) as ICommand;
        }

        public static void SetCommand(Button button, ICommand command)
        {
            button.SetValue(ButtonClickCommandProperty, command);
        }

        private static void OnSetCommandCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                var behavior = GetOrCreateBehavior(button);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static ButtonClickBehavior GetOrCreateBehavior(Button button)
        {
            var behavior = button.GetValue(ButtonClickCommandBehaviorProperty) as ButtonClickBehavior;
            if (behavior == null)
            {
                behavior = new ButtonClickBehavior(button);
                button.SetValue(ButtonClickCommandBehaviorProperty, behavior);
            }
            return behavior;
        }
    }
}
